package com.ts.hidden.fragment.login;

import android.app.Activity;
import android.app.Fragment;
import android.graphics.drawable.AnimationDrawable;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.ts.hidden.R;

public class CreateFragment extends Fragment implements OnClickListener {

    private ImageView imgNext;
    private String currentPw = "";
    private TextView txtPassword;

    public static CreateFragment newInstance(String s) {
        CreateFragment f = new CreateFragment();
        return f;
    }
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if(savedInstanceState != null) {
            currentPw = savedInstanceState.getString("pass");
        }
    }
    
    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        if(!TextUtils.isEmpty(txtPassword.getText())) {
            outState.putString("pass", txtPassword.getText().toString());
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.create_fragment, container, false);
        initLayout(rootView);
        initListener();
        updatePassword();
        return rootView;
    }

    private void initLayout(View rootView) {
        imgNext = (ImageView) rootView.findViewById(R.id.login_page2_next);
        AnimationDrawable animationDrawable = (AnimationDrawable) imgNext.getBackground();
        animationDrawable.start();

        rootView.findViewById(R.id.btn_num_1).setOnClickListener(this);
        rootView.findViewById(R.id.btn_num_2).setOnClickListener(this);
        rootView.findViewById(R.id.btn_num_3).setOnClickListener(this);
        rootView.findViewById(R.id.btn_num_4).setOnClickListener(this);
        rootView.findViewById(R.id.btn_num_5).setOnClickListener(this);
        rootView.findViewById(R.id.btn_num_6).setOnClickListener(this);
        rootView.findViewById(R.id.btn_num_7).setOnClickListener(this);
        rootView.findViewById(R.id.btn_num_8).setOnClickListener(this);
        rootView.findViewById(R.id.btn_num_9).setOnClickListener(this);
        rootView.findViewById(R.id.btn_num_0).setOnClickListener(this);
        rootView.findViewById(R.id.btn_clear).setOnClickListener(this);
        
        txtPassword = (TextView) rootView.findViewById(R.id.create_txt_password1);
    }

    private void initListener() {
        imgNext.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
        case R.id.login_page2_next:
            if(TextUtils.isEmpty(currentPw)) {
                Toast.makeText(getActivity(), "ERROR >>> Password is empty", Toast.LENGTH_SHORT).show();
            } else {
                listener.onICreateNext(currentPw);
            }
            break;
        case R.id.btn_num_1:
            currentPw += "1";
            break;
        case R.id.btn_num_2:currentPw += "2";
            break;
        case R.id.btn_num_3:currentPw += "3";
            break;
        case R.id.btn_num_4:currentPw += "4";
            break;
        case R.id.btn_num_5:currentPw += "5";
            break;
        case R.id.btn_num_6:currentPw += "6";
            break;
        case R.id.btn_num_7:currentPw += "7";
            break;
        case R.id.btn_num_8:currentPw += "8";
            break;
        case R.id.btn_num_9:currentPw += "9";
            break;
        case R.id.btn_num_0:currentPw += "0";
            break;
        case R.id.btn_clear:currentPw = "";
            break;

        default:
            break;
        }
        
        updatePassword();
    }

    public interface ICreateListener {
        void onICreateNext(String currentPw);
    }

    private ICreateListener listener;

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        if (activity instanceof ICreateListener) {
            listener = (ICreateListener) activity;
        }
    }

    @Override
    public void onDetach() {
        super.onDetach();
        listener = null;
    }
    
    private void updatePassword() {
        txtPassword.setText(currentPw);
    }
}
